package com.hxkj.utils;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author huangzhaoyong
 * @ClassName Common
 * @Description TODO
 * @Date 2018/7/11 14:54
 * @Version 1.0
 */
public class CommonUtils {

    public static<T> Map<String, List<T>> listToMapList(List<T> list, String key) throws Exception{
        Map<String, List<T>>  result = new HashMap<>();
        if(null == list || StringUtils.isEmpty(key)){
            return result;
        }
        List<T> tList;
        Class cls;
        Field[] fields;
        String value;
        Map map;
        for(T t: list){
            tList = new ArrayList<>();

            if(t instanceof Map){
                map = (Map) t;
                if(null == map.get(key)){
                    continue;
                }
                value = map.get(key).toString();
                if(null != result.get(value)){
                    tList = result.get(value);
                }

                if(map.keySet().contains(key) && null != map.get(key)){
                    tList.add(t);
                }
                result.put(value, tList);
                continue;
            }

            cls = t.getClass();
            //得到所有属性
            fields = cls.getDeclaredFields();
            for(Field field: fields){
                //打开私有访问
                field.setAccessible(true);
                //获取属性
                String name = field.getName();
                if(!name.equals(key) || null == field.get(t)){
                    continue;
                }
                value = field.get(t).toString();
                if(null != result.get(value)){
                    tList = result.get(value);
                }
                tList.add(t);
                result.put(value, tList);
            }
        }
        return result;
    }

    public static<T> Map<String, T> listToMap(List<T> list, String key) throws Exception{
        Map<String, T>  result = new HashMap<>();
        if(null == list || StringUtils.isEmpty(key)){
           return result;
        }
        Class cls;
        Field[] fields;
        String value;
        for(T t: list){
            cls = t.getClass();

            if(t instanceof Map){
                if(((Map) t).keySet().contains(key) && null != ((Map) t).get(key)){
                    result.put(((Map) t).get(key).toString(), t);
                }
                continue;
            }

            //得到所有属性
            fields = cls.getDeclaredFields();
            for(Field field: fields){
                //打开私有访问
                field.setAccessible(true);
                //获取属性
                String name = field.getName();
                if(!name.equals(key) || null == field.get(t)){
                    continue;
                }
                value = field.get(t).toString();
                result.put(value, t);
            }
        }
        return result;
    }
}
